# coding=utf-8
import time

import flask
import json
from flask import request, g
from Arm_Lib import Arm_Device
import RPi.GPIO as GPIO

# 创建机械臂对象
Arm = Arm_Device()
server = flask.Flask(__name__)


@server.route('/api/reset_arm', methods=['GET'])
def reset_arm():
    Arm.Arm_serial_servo_write6(90, 90, 90, 90, 90, 90, 600)
    # for i in range(6):
    #     Arm.Arm_serial_servo_write(i + 1, 90, 1000)
    #     time.sleep(.01)
    return 'ok'


@server.route('/api/start_study', methods=['GET'])
def start_study():
    Arm.Arm_Button_Mode(1)
    return 'ok'


@server.route('/api/finish_study', methods=['GET'])
def finish_study():
    Arm.Arm_Button_Mode(0)
    return 'ok'


@server.route('/api/get_position', methods=['GET'])
def get_position():
    pos = []
    for i in range(6):
        aa = Arm.Arm_serial_servo_read(i + 1)
        pos.append(aa)
        time.sleep(.01)
    return json.dumps(pos, ensure_ascii=False)


@server.route('/api/move_all', methods=['GET'])
def move_all():
    data = eval(request.get_data(as_text=True))
    position = data['position']
    # Arm.Arm_serial_servo_write6(position[0], position[1], position[2], position[3], position[4], position[5], 400)
    for i in range(6):
        Arm.Arm_serial_servo_write(i + 1, int(position[i]), 600)
        time.sleep(.01)
    return 'ok'


@server.route('/api/move_one', methods=['GET'])
def move_one():
    data = eval(request.get_data(as_text=True))
    index = data['index']
    angle = data['angle']
    Arm.Arm_serial_servo_write(index, angle, 600)
    return 'ok'


@server.route('/api/set_rgb', methods=['GET'])
def set_rgb():
    data = eval(request.get_data(as_text=True))
    rgb = data['rgb']
    Arm.Arm_RGB_set(int(rgb[0]), int(rgb[1]), int(rgb[2]))
    return 'ok'


@server.route('/api/set_pwm', methods=['GET'])
def set_pwm():
    data = eval(request.get_data(as_text=True))
    index = int(data['index'])
    angle = int(data['angle'])
    Arm.Arm_PWM_servo_write(index, angle)
    return 'ok'


@server.route('/api/set_gpio', methods=['GET'])
def set_gpio():
    data = eval(request.get_data(as_text=True))
    index = int(data['bcm_code'])
    value = int(data['value'])
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(index, GPIO.OUT)
    if value == 0:
        GPIO.output(index, GPIO.LOW)
    else:
        GPIO.output(index, GPIO.HIGH)
    # time.sleep(1000)
    # GPIO.cleanup()
    return 'ok'


@server.route('/api/read_input', methods=['GET'])
def read_input():
    data = eval(request.get_data(as_text=True))
    input_pin = int(data['input_pin'])
    output_pin = int(data['output_pin'])
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(input_pin, GPIO.IN)
    GPIO.setup(output_pin, GPIO.OUT)
    input_result = GPIO.input(input_pin)
    GPIO.output(output_pin, input_result)
    return 'ok'


@server.route('/api/step_motor', methods=['GET'])
def step_motor():
    print('set motor')
    data = eval(request.get_data(as_text=True))
    enable_pin = int(data['enable_pin'])
    direction_pin = int(data['direction_pin'])
    step_pin = int(data['step_pin'])
    enable = int(data['enable'])
    direction = int(data['direction'])
    step = int(data['step'])
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(enable_pin, GPIO.OUT)
    GPIO.setup(direction_pin, GPIO.OUT)
    GPIO.setup(step_pin, GPIO.OUT)
    GPIO.output(enable_pin, GPIO.LOW)

    if direction == 1:
        GPIO.output(direction_pin, GPIO.HIGH)
        print("clock")
        for i in range(0, 200 * step):
            GPIO.output(step_pin, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(step_pin, GPIO.LOW)
            time.sleep(0.001)
    elif direction == 2:
        GPIO.output(direction_pin, GPIO.LOW)
        for i in range(0, 200 * step):
            GPIO.output(step_pin, GPIO.HIGH)
            time.sleep(0.001)
            GPIO.output(step_pin, GPIO.LOW)
            time.sleep(0.001)
    return 'ok'


if __name__ == '__main__':
    server.run(debug=True, port=6666, host='0.0.0.0')
